/**

 @author 石头哥哥
 </P>
 Date:   2014/12/17
 </P>
 Time:   21:06

 注解：

 */
package GameObject {
import Qbase.utils.sound.SoundManager;

import flash.geom.Point;

import lzm.starling.STLConstant;

import starling.animation.IAnimatable;

import starling.core.Starling;
import starling.display.DisplayObject;
import starling.display.MovieClip;
import starling.display.Sprite;
import starling.textures.Texture;

public class Hero extends Sprite implements IAction {

    /**
     *  当前血量
     */
    private var _hp:int;


    public function get hp():int {
        return _hp;
    }

    public function set hp(value:int):void {
        _hp = value;
    }

    /**
     *  子弹类型
     */
    private var bulletType:int;


    /**
     * 子弹移动速度
     */
    private var bulletSpeed:int = 0;

    /**
     * 子弹贴图类型
     */
    private var type:int = 0;
    /**
     *
     */
    private var obj:Sprite;


    private var heroAnimate:MovieClip;

    /**
     * 可造成的伤害值  碰撞
     */
    private var _hurt:int;

    public function get hurt():int {
        return _hurt;
    }


    /**
     * 创建
     * @param type   飞机类型
     * @param hp
     * @param bulletType
     * @param bulletSpeed
     */
    public function Hero(obj:Sprite = null, type:int = 1, hp:uint = 100, bulletType:int = 2, bulletSpeed:int = -20) {
        super();
        this.type = type;
        this._hp = hp;
        this.bulletType = bulletType;
        this.bulletSpeed = bulletSpeed;
        this._hurt = HurtByType(this.type);

        this.x = (STLConstant.StageWidth - this.width) / 2;
        this.y = 540;

        this.obj = obj;

        this.init();

    }


    /**
     * 根据贴图类型 初始化  对象
     *
     * 注意当前对象要添加到 游戏场景中
     */
    public function init():void {
        //add animate of hero
        var result0:Vector.<Texture> = new Vector.<Texture>();
        for (var i:int = 1; i <= 2; ++i) {
            result0.push(GameSystem.assetManager.getTexture("hero" + i))
        }
        heroAnimate = new MovieClip(result0);
        heroAnimate.touchable = false;
        addChild(heroAnimate);
        Starling.juggler.add(heroAnimate);
    }


    /**
     *
     * @param type
     */
    public static function HurtByType(type:int):int {
        if (type == 1)return 2;
        if (type == 2)return 2;
        if (type == 3)return 3;
        return -1;
    }


    /**
     * 创建子弹对象
     * @return
     */
    public function fireX():Bullet {
        var bullet:Bullet = new Bullet(this.bulletType,
                bulletSpeed, hurt);
        if (bullet) {
            bullet.init();
            bullet.x = this.x + this.width / 2;
            bullet.y = this.y;
            bullet.touchable = false;
            GameManger.heroBulletList.push(bullet);
            Starling.juggler.add(bullet);//begin fire
            return bullet;
        }
        return null;
    }


    private var fireAnimatable:IAnimatable;

    /**
     *  fire
     */
    public function fire():void {
        fireAnimatable = Starling.juggler.repeatCall(fire0, 0.2);
        trace("begin enemy height:", this.height);
    }

    /**
     *
     */
    private function fire0():void {
        if (!fireAnimatable) {
            throw Error("");
        }
        var temp:int = this.width / 2;
        if (GameManger.fireType == 1) {
            var bullet:Bullet = new Bullet(this.bulletType,
                    bulletSpeed, hurt);
            if (bullet) {
                bullet.init();

                bullet.x = this.x + temp;
                bullet.y = this.y;

                bullet.touchable = false;

                obj.addChild(bullet);// add  enemy

                //manager of bullet
                GameManger.heroBulletList.push(bullet);
                Starling.juggler.add(bullet);//begin fire
            }
        } else if (GameManger.fireType == 3) {
            var offset:int = 0;
            for (var i = 0; i != GameManger.fireType; ++i) {
                var bullet:Bullet = new Bullet(this.bulletType,
                        bulletSpeed, hurt);
                if (bullet) {
                    bullet.init();

                    bullet.x = this.x + offset;
                    bullet.y = this.y;
                    offset += temp;

                    bullet.touchable = false;

                    obj.addChild(bullet);// add  enemy

                    //manager of bullet
                    GameManger.heroBulletList.push(bullet);
                    Starling.juggler.add(bullet);//begin fire
                }
            }
        } else if (GameManger.fireType == 2) {
            temp = this.width;
            var offset:int = 0;
            for (var i = 0; i != GameManger.fireType; ++i) {
                var bullet:Bullet = new Bullet(this.bulletType,
                        bulletSpeed, hurt);
                if (bullet) {
                    bullet.init();

                    bullet.x = this.x + offset;
                    bullet.y = this.y;
                    offset += temp;

                    bullet.touchable = false;

                    obj.addChild(bullet);// add  enemy

                    //manager of bullet
                    GameManger.heroBulletList.push(bullet);
                    Starling.juggler.add(bullet);//begin fire
                }
            }
        }
        SoundManager.getInstance().playSound("sound/bullet.mp3");
    }


    /**
     * 停止游戏
     */
    public function pause():void {
        //mc of hero
        if (heroAnimate)Starling.juggler.remove(heroAnimate);
        // fire
        if (fireAnimatable)Starling.juggler.remove(fireAnimatable);

    }

    /**
     * 恢复游戏状态
     */
    public function reuse():void {
        if (heroAnimate)Starling.juggler.add(heroAnimate);
        // fire
        if (fireAnimatable)Starling.juggler.add(fireAnimatable);

    }


    /**
     * 移除显示对象
     */
    public function destroy(dispose:Boolean = false):Boolean {
        if (_hp <= 0) {
            removeFromParent(dispose);
            return true;
        }
        return false;
    }


    /**
     * clear obj of this
     * this.y max limit is STLConstant.StageHeight+10
     */
    public function clear(dispose:Boolean = true):void {
        this.removeFromParent(dispose);
        if (fireAnimatable) {
            Starling.juggler.remove(fireAnimatable);
            fireAnimatable = null;
        }
        if (heroAnimate)Starling.juggler.remove(heroAnimate);
        this.dispose();
    }

    /**
     *
     * @param target
     * @return
     */
    public function collision(target:DisplayObject):Boolean {
        var raid:int = Math.max(this.width, this.height) / 2 + Math.max(target.width, target.height) / 2;//半径之和
        if (Point.distance(new Point(this.x + this.width / 2, this.y + this.height / 2),
                new Point(target.x + target.width / 2, target.y + target.height / 2)) <= raid) {
            if (target is Bullet) {
                _hp -= Bullet(target).hurt;
            }
            if (target is Enemy) {
                _hp -= Enemy(target).hurt;
            }
            return true;
        }
        return false;
    }
}
}
